/**
  ******************************************************************************
  * @file    gt32f030_i2c.c
  * @author  GT Application Team
  * @version V1.0.0
  * @date    03-January-2025      
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 Giantec Semicondutor Inc</center></h2>
  *
  *             http://www.giantec-semi.com/
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/ 
#include "gt32f030_i2c.h"
#include "gt32f030_rcc.h"
/** @addtogroup GT32F030_StdPeriph_Driver
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define  I2C_CR_ENABLE          ((uint32_t)0x00000040)
#define  I2C_CR_HIGHSPEED       ((uint32_t)0x00000001)
#define  I2C_CR_Start           ((uint32_t)0x00000020)
#define  I2C_CR_Stop            ((uint32_t)0x00000010)
#define  I2C_CR_ACK             ((uint32_t)0x00000004)
#define  I2C_CR_IT              ((uint32_t)0x00000008)
#define  I2C_CR_OperationMask   ((uint32_t)0x00000074)

#define  I2C_TM_RUN             ((uint32_t)0x00000001)
#define  I2C_ADDRMask           ((uint32_t)0x000000FE)
#define  I2C_ADDR_ENGC          ((uint32_t)0x00000001)

//#define  I2C_Start_Enable           ((uint32_t)0x00000020)
//#define  I2C_Stop_Enable            ((uint32_t)0x00000010)
//#define  I2C_ACK_Enable             ((uint32_t)0x00000004)

//#define  I2C_Start_Disable           ((uint32_t)0x00000000)
//#define  I2C_Stop_Disable            ((uint32_t)0x00000000)
//#define  I2C_ACK_Disable             ((uint32_t)0x00000000)

/** @defgroup I2C 
  * @brief I2C driver modules
  * @{
  */ 

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup I2C_Private_Functions
  * @{
  */ 

/** @defgroup I2C_Init Initialization functions
 *  @brief   Initialization and functions
 *
@verbatim    
 ===============================================================================
              ##### Initialization functions #####
 ===============================================================================
@endverbatim
  * @{
  */
/**
  * @brief  Deinitializes the I2C peripheral registers to their default reset values.
  * @retval None
  */
void I2C_DeInit(void)
{
	RCC_PeriphResetCmd(RCC_PeriphReset_I2C, ENABLE);
	RCC_PeriphResetCmd(RCC_PeriphReset_I2C, DISABLE);
}


/**
  * @brief  Initializes the I2C peripheral according to the specified 
  *         parameters in the I2C_InitStruct.
  * @param  I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
  *         contains the configuration information.
  * @retval None
  */
void I2C_Init(I2C_InitTypeDef* I2C_InitStruct)
{
	uint32_t tp1 = 0,tp2 = 0; 
	/* Check the parameters */
	assert_param(IS_FUNCTIONAL_STATE(I2C_InitStruct->I2C_HighSpeedCmd));
	assert_param(IS_FUNCTIONAL_STATE(I2C_InitStruct->I2C_BuadCmd));
	assert_param(IS_I2C_BUADCFG(I2C_InitStruct->I2C_BuadCfg));
	assert_param(IS_I2C_ADDRESS(I2C_InitStruct->I2C_Address));

	tp1  = I2C->ADDR;
	tp1 &=~I2C_ADDRMask;
	tp1 |= I2C_InitStruct->I2C_Address<<1;
	I2C->ADDR = tp1;
	
	
	I2C->BAUDCR  = I2C_InitStruct->I2C_BuadCfg;
	I2C->TMRUN   = I2C_TM_RUN;
	
	tp2 = 0;
	if (I2C_InitStruct->I2C_HighSpeedCmd)
		 tp2 |= I2C_CR_HIGHSPEED;  //High Speed
	else
		 tp2 &=~I2C_CR_HIGHSPEED;  //Low Speed
		
	I2C->CR = tp2;
}

/**
  * @brief  Fills each I2C_InitStruct member with its default value.
  * @param  I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
  * @retval None
  */
void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
{
    /*---------------- Reset I2C init structure parameters values ----------------*/
    I2C_InitStruct->I2C_HighSpeedCmd     = DISABLE;
    I2C_InitStruct->I2C_BuadCmd = DISABLE;
    I2C_InitStruct->I2C_BuadCfg = 0x01;
    I2C_InitStruct->I2C_Address = 0x00;
}

/**
  * @}  
  */





/** @defgroup I2C_OpCfg Operation and Configuration functions
 *  @brief   Operation and Configuration functions
 *
@verbatim   
 ===============================================================================
           ##### Operation and Configuration functions #####
 ===============================================================================  
@endverbatim
  * @{
  */

/**
  * @brief  Enables or disables the SPI .
  * @param  NewState: new state of the SPI.
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void I2C_Cmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));
	
  if (NewState != DISABLE)
  {
    I2C->CR  |= (uint32_t)I2C_CR_ENABLE;
  }
  else
  {
    I2C->CR  &= (uint32_t)(~I2C_CR_ENABLE);
  }
}

/**
  * @brief  Configure the next action performed by the IIC hardware.
	* @param  enable: Whether Enable needs to be performed.
	* @param  Sta: Whether Start-action needs to be performed.
	* @param  Sto: Whether Stop-action needs to be performed.
	* @param  Ack: Whether Ack-action needs to be performed.
  * @retval None
  */
void I2C_OperationConfig(uint8_t Enable,uint8_t Sta,uint8_t Sto,uint8_t Ack)
{
	uint32_t tp1 = 0; 
	/*tp1  = I2C->CR;
	tp1 &=~I2C_CR_OperationMask;
	tp1 |= (Enable<<6) | (Sta<<5) | (Sto<<4) | (Ack<<2) ;*/
	
	tp1  = I2C->CR&0x1;
	tp1 |= (Enable<<6) | (Sta<<5) | (Sto<<4) | (Ack<<2) ;
	I2C->CR = tp1;
}

/**
  * @brief  Enables or disables the I2C General Call function.
  * @param  NewState: new state of the UART.
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void I2C_GeneralCallCmd(FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_FUNCTIONAL_STATE(NewState));

	if (NewState != DISABLE)
			I2C->ADDR |= I2C_ADDR_ENGC;
	else
			I2C->ADDR &=~ I2C_ADDR_ENGC;
}

/**
  * @brief  Sends a data byte through the I2C peripheral.
  * @param  Data: Byte to be transmitted.
  * @retval None
  */
void I2C_SendData(uint8_t Data)
{
   I2C->DATA = Data;
}

/**
  * @brief  Returns the most recent received data by the I2C peripheral.
  * @param  None
  * @retval The value of the received data.
  */
uint8_t I2C_ReceiveData(void)
{
   return (uint8_t)I2C->DATA;
}


/**
  * @}  
  */



/** @defgroup  I2C_IntrFlag Interrupts and flags management functions
 *  @brief    Interrupts and flags management functions
 *
@verbatim   
 ===============================================================================
           #####  Interrupts and flags management functions #####
 ===============================================================================  
@endverbatim
  * @{
  */
/**
  * @brief  Checks whether the specified I2C interrupt flag is set or not.
  * @param  None
  * @retval The new state of I2C interrupt flag  (SET or RESET).
  */
ITStatus I2C_GetIT(void)
{
  ITStatus bitstatus = RESET;

  if ((I2C->CR & I2C_CR_IT) != RESET)
    bitstatus = SET;
  else
    bitstatus = RESET;
	
  return  bitstatus;
}

/**
  * @brief  Clears the I2C interrupt flag.
  * @param  None
  * @retval None
  */
void I2C_ClearIT(void)
{
  I2C->CR &=~ I2C_CR_IT;
}

/**
  * @brief  Returns the I2C status.
  * @param  None
  * @retval The I2C status.
  */
uint16_t I2C_GetStatus(void) { 
  return (uint16_t)(I2C->SR & 0x00FF);
}

/**
  * @brief  Checks whether the specified I2C flag is set or not.
  * @param  I2C_Flag: specifies the flag to check.
  * @retval The new state of I2C flag (SET or RESET).
  */
FlagStatus I2C_GetFlagStatus(uint32_t I2C_Flag) {
  FlagStatus bitStatus = RESET;
  assert_param(IS_I2C_FLAG_STATUS(I2C_Flag));
  if ((I2C->SR & I2C_Flag) != (uint32_t)RESET) {
    bitStatus = SET;
  } else {
    bitStatus = RESET;
  }
  return bitStatus;
}

/**
  * @}  
  */ 

/**
  * @}  
  */

/**
  * @}  
  */ 

/**
  * @}
  */ 
/************************ (C) COPYRIGHT Giantec Semicondutor Inc *****END OF FILE****/
